package i2.application.checklist.entities;

import static javax.persistence.GenerationType.AUTO;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;



@Entity
@Table(name = "application", uniqueConstraints = @UniqueConstraint(columnNames = "nom"))
@NamedQueries ({
	@NamedQuery(name = "Application.isNameAlreadyUsed", query = "select 1 from Application where nom = :nom AND id != :id"),
	@NamedQuery(name = "Application.hasLivraisons", query = "select 1 from Application a, Livraison l where a.id = :id and l.application.id = a.id")
})
public class Application implements Serializable  {

	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = AUTO)
	@Column(name = "id", unique = true, nullable = false)
	private int id;

	@Column(name="nom", nullable = false, length = 45)
	@NotNull(message="le nom est obligatoire")
	@NotEmpty(message="le nom ne peut pas être vide")
	@Size(max=45)
	private String nom;
	
	@Column(name = "description")
	private String description;
	
	public Application() {
	}
	
	public Application(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

    @Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		Application other = (Application) obj;
		if (id != other.id){
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Application [id=" + id + ", nom=" + nom + ", description="
				+ description + "]";
	}
	
//	@PrePersist
//	public void checkNameIsUnique(){
//		//  TODO mettre la validation ICI !!! Bah c'est pas cool d'injecter EM ici..... ou alors il faut supprimer les DAO
//		Query query = entityManager.createNamedQuery("Application.isNameAlreadyUsed");
//		query.setParameter("nom", application.getNom());
//		
//		Object isUnique = null;
//		try {
//			isUnique = query.getSingleResult();
//			logger.debug("le nom existe déjà ? : {}",isUnique);
//		} catch (NoResultException e) {
//			return false;
//		}
//		return true;
//	}
    
}
